package com.baymax.test.scene.demo

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Shader
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addText
import com.baymax.test.scene.newSceneFlexContainer
import com.baymax.test.scene.newScrollContainer
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.util.Date
import java.util.Locale

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       TextInputScene
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/7/15 14:47
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class TextShaderScene : BaseScene() {
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {

        return p1.newScrollContainer {
            val text =
                object : TextView(context) {
                    var index = 0
                    init {
                        textSize = 80f
                        val p = paint
                        val now = Date()
                        val formatter = SimpleDateFormat("HH:mm:ss", Locale.CHINA);
                        text = formatter.format(now)
                        launch {
                            while (true) {
                                delay(1000)
                                now.time += 1000
                                index++
                                text = formatter.format(now)
                            }
                        }

                    }

                    val linear: LinearGradient by lazy {
                        val s = Color.argb(10, 0, 0, 0)
                        val s1 = Color.argb(80, 255, 0, 0)
                        val e = Color.argb(80, 0, 0, 0)
                        LinearGradient(
                            0f,
                            100f,
                            0f,
                            textSize,
                            intArrayOf(s,s1 , e),
                            floatArrayOf(0f,0.4f, 1f),
                            Shader.TileMode.CLAMP
                        )
                    }


                    override fun onDraw(canvas: Canvas) {
                        if (index.mod(3) == 0) {
                            paint.setShader(null)
                        } else {
                            paint.setShader(linear)
                        }
                        super.onDraw(canvas)

                    }

                }
            addView(text)
        }
    }
}